package net.comm.tags;

import java.util.List;


import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.ValueMaping;
import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;
import net.comm.group.bean.BaseGPicture;
import net.comm.group.service.impl.GAlbumServiceImpl;
import net.comm.group.service.impl.GMemberServiceImpl;
import net.comm.group.service.impl.GPictureServiceImpl;
import net.comm.group.service.impl.GpartyServiceImpl;
import net.comm.group.service.impl.GroupsServiceImpl;
import net.comm.group.web.form.GAlbumForm;
import net.comm.group.web.form.GMemberForm;
import net.comm.group.web.form.GPictureForm;
import net.comm.group.web.form.GpartyForm;
import net.comm.group.web.form.GroupsForm;
import net.comm.message.service.impl.MsgcontServiceImpl;
import net.comm.message.web.form.MsgcontForm;
import net.comm.page.PageCtr;
import net.comm.param.service.impl.CnpcaServiceImpl;
import net.comm.param.service.impl.PropertyServiceImpl;
import net.comm.param.web.form.PropertyForm;
import net.comm.shequ.service.impl.ShequServiceImpl;
import net.comm.shequ.web.form.ShequForm;
import net.comm.user.service.impl.DetailsServiceImpl;
import net.comm.user.service.impl.UserHomeServiceImpl;
import net.comm.user.service.impl.UserSysinfoServiceImpl;
import net.comm.user.service.impl.UsersServiceImpl;
import net.comm.user.web.form.DetailsForm;
import net.comm.user.web.form.UserHomeForm;
import net.comm.user.web.form.UserSysinfoForm;
import net.comm.user.web.form.UsersForm;
import net.comm.util.DatetimeUtil;

public class JstlFun {

	/*日期格式化输出
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：YYYY-MM-DD HH:MM
	 */
	public static String FormatDate14(String date, String split){
		
	   return DatetimeUtil.FormatDate14(date, split);
	}
	/*日期格式化输出
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：MM-DD, MM/DD
	 */
	public static String FormatMMDD14(String date, char split){
		
	   return DatetimeUtil.FormatMMDD14(date, split);
	}
	/*日期格式化输出
	 * 输入：YYYYMMDD
	 * 输出：MM/DD MM-DD
	 */
	public static String FormatMMDD(String date, char split){
		
	   return DatetimeUtil.FormatMMDD(date, split);
	}
	/*日期格式化输出
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：YY/MM/DD YY-MM-DD
	 */
	public static String FormatYYYYMMDD(String date,  char split){
		
	   return DatetimeUtil.FormatYYYYMMDD(date, split);
	}

	/*日期格式化输出
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：MM-DD HH:MM 
	 */
	public static String FormatMDHM(String date){
		
	   return DatetimeUtil.FormatMDHM(date);
	}
	/*日期格式化输出
	 * 输入：YYYYMMDD
	 * 输出：YYYY-MM-DD 
	 */
	public static String Formatdate8(String date){
		
	   return DatetimeUtil.FormatDate8(date);
	}
	/*时间格式化输出
	 * 输入：HHMMSS
	 * 输出：HH:MM:SS 
	 */
	public static String FormatTime(String time){
		
	   return DatetimeUtil.FormatTime(time, ':');
	}
	/*返回距离当前日期的天数
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：YY/MM/DD YY-MM-DD
	 */
	public static long GetQuot(String date){
		
	   return DatetimeUtil.getQuot(date, DatetimeUtil.getDatetime());
	}
	
	/* 判断系统时间是否超过当前日期
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：YY/MM/DD YY-MM-DD
	 */
	public static boolean BjudgeRange(String date1){
		
	   return DatetimeUtil.bjudgeRange(DatetimeUtil.getDatetime(), date1 );
	}
	
	/*返回距离当前时间的差
	 * 输入：YYYYMMDDHHMMSS
	 * 输出：XX天XX小时XX分XX秒
	 */
	public static String GetQuotDHMS(String date){
		
		if(date == null ||date.equals("")){
			return "";
		}
		if(date.length() < 14){
			return date;
		}
		long qu = DatetimeUtil.getQuot(DatetimeUtil.getDatetime(), date);
		//判断与当前日期的差
		if(qu  < 1){
			//小于1天，则返回时间差
			return DatetimeUtil.getQuotDHMS(DatetimeUtil.getDatetime(), date) + "前";
		}else if(qu >= 1 && qu < 10){
			//大于1天，则返回几天前
			return String.valueOf(qu) + "天前";
		}else {
			return DatetimeUtil.FormatDate14(date, "-");
		}		
	}	
	
	/*根据会员ID取上次登录日期
	 * 输入：UserID
	 * 输出：LTime
	 */
	public static String GetLTime(int userID){
		
		UsersForm theForm = new UsersForm();
		theForm.setUserID(userID);

		UsersServiceImpl service = new UsersServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return DatetimeUtil.FormatDate14(theForm.getLtime(),"-");
	}
	/*根据省市县ID 取名称
	 * 输入：id
	 * 输出：Name
	 */
	public static String GetCnpcaName(int id){
		
		if(id ==0){
			return "";
		}
		CnpcaServiceImpl service = new CnpcaServiceImpl();
		
		return service.getById(id);
	}		
	
	/*根据会员ID取昵称
	 * 输入：UserID
	 * 输出：Integral
	 */
	public static int GetUserIntegral(int userID){
		
		
		UserSysinfoForm theForm = new UserSysinfoForm();
		theForm.setUserID(userID);

		UserSysinfoServiceImpl service = new UserSysinfoServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
			
		return theForm.getIntegral();
	}
	
	/*根据会员ID取昵称
	 * 输入：UserID
	 * 输出：nick
	 */
	public static String GetUserNick(int userID){
		
		
		DetailsForm theForm = new DetailsForm();
		theForm.setUserID(userID);

		DetailsServiceImpl service = new DetailsServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		if(theForm.getNick().equals("")){
			
			UsersForm userForm = new UsersForm();
			userForm.setUserID(userID);

			UsersServiceImpl userservice = new UsersServiceImpl();
			
			userservice.get(userForm, msg);		
			return userForm.getNick();
		}else{
			
			return theForm.getNick();
		}
	}
	/*根据会员ID取昵称
	 * 输入：UserID
	 * 输出：nick
	 */
	public static String GetUserPic(int userID){
		
		
		DetailsForm theForm = new DetailsForm();
		theForm.setUserID(userID);

		DetailsServiceImpl service = new DetailsServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		if(theForm.getPicID() > 0){
			
			return GetPic(theForm.getPicID());		
		}else{
			return "images/nopic.gif";
		}
	}	
	/*根据会员ID取真实姓名
	 * 输入：UserID
	 * 输出：name
	 */
	public static String GetUserName(int userID){
		
		DetailsForm theForm = new DetailsForm();
		theForm.setUserID(userID);

		DetailsServiceImpl service = new DetailsServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return theForm.getName();
	}

	
	/*根据文件ID取文件
	 * 输入：UserID
	 * 输出：pic
	 */
	public static String GetPic(int f_id){
		
		if(f_id > 0){
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			ReturnMessage msg  = new ReturnMessage();		
			
			filesForm.setF_ID(f_id);
			
			fileservice.get(filesForm, msg);
			
			if(filesForm.getF_Zip() == GlbFinal.ZIP_600){
				
				return filesForm.getFolder() + filesForm.getF_name();
			}else{
				
				return filesForm.getFolder() + "zip" + filesForm.getF_name();
			}			
		}else{
			return "images/nopic.gif";
		}		
	}
	/*根据文件ID取文件
	 * 输入：UserID
	 * 输出：pic
	 */
	public static String GetBigPic(int f_id){
		
		if(f_id > 0){
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			ReturnMessage msg  = new ReturnMessage();		
			
			filesForm.setF_ID(f_id);
			
			fileservice.get(filesForm, msg);
			
			return filesForm.getFolder() + filesForm.getF_name();
		}else{
			return "images/0_bg1.jpg";
		}		
	}
	
	/*根据文件ID取文件描述信息
	 * 输入：UserID
	 * 输出：pic
	 */
	public static String GetFileInfo(int f_id){
		
		if(f_id > 0){
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			ReturnMessage msg  = new ReturnMessage();		
			
			filesForm.setF_ID(f_id);
			
			fileservice.get(filesForm, msg);
			
			return filesForm.getF_info();		
		}else{
			return "";
		}		
	}	
	
	/*根据会员ID取会员所在省
	 * 输入：UserID
	 * 输出：Work_location
	 */
	public static int GetUserLocation(int userID){
		
		UserHomeForm theForm = new UserHomeForm();
		theForm.setUserID(userID);

		UserHomeServiceImpl service = new UserHomeServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return theForm.getWork_location();
	}	
	/*根据会员ID取会员所在市
	 * 输入：UserID
	 * 输出：Work_sublocation
	 */
	public static int GetUserSubLocation(int userID){
		
		UserHomeForm theForm = new UserHomeForm();
		theForm.setUserID(userID);

		UserHomeServiceImpl service = new UserHomeServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return theForm.getWork_sublocation();
	}
	/*根据社区ID取群社区名称
	 * 输入：VillageID
	 * 输出：Village
	 */
	public static String GetVillageName(int VillageID){
		
		ShequServiceImpl service = new ShequServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		

		ShequForm theForm = new ShequForm();
		theForm.setVillageID(VillageID);

		service.get(theForm, msg);
		
		return theForm.getVillage();
	}	
	
	/*根据群组ID取群组名称
	 * 输入：GroupID
	 * 输出：GroupName
	 */
	public static String GetGroupName(int groupID){
		
		if(groupID == GlbFinal.PUBLICID){
			
			return "会员";
		}
		
		GroupsForm theForm = new GroupsForm();
		theForm.setGroupID(groupID);

		GroupsServiceImpl service = new GroupsServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.getgroup(theForm, msg);
		
		return theForm.getName();
	}

	/*根据群组ID取群组名称
	 * 输入：GroupID
	 * 输出：GroupName
	 */
	public static String GetPartyTitle(int partyID){
		
		if(partyID == 0){
			
			return "";
		}
		
		GpartyForm theForm = new GpartyForm();
		theForm.setPartyID(partyID);

		GpartyServiceImpl service = new GpartyServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return theForm.getTitle();
	}
	/*根据群组ID取群组海报
	 * 输入：GroupID
	 * 输出：GroupPIC
	 */
	public static String GetPartySmallPic(int partyID){
		
		if(partyID == 0){
			
			return "images/img5.jpg";
		}
		
		GpartyForm theForm = new GpartyForm();
		theForm.setPartyID(partyID);

		GpartyServiceImpl service = new GpartyServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		if(theForm.getSmallPic() > 0){
			
			return GetPic(theForm.getSmallPic());
		}else{
			
			return "images/img5.jpg";
		}
	}

	/*根据msgID取短信内容
	 * 输入：msgID
	 * 输出：Msg
	 */
	public static String GetMsgcont(int msgID){
		
		MsgcontForm theForm = new MsgcontForm();
		theForm.setMsgID(msgID);

		MsgcontServiceImpl service = new MsgcontServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		return theForm.getMsg();
	}	
	/*根据群组ID取群组图标
	 * 输入：GroupID
	 * 输出：GroupPIC
	 */
	public static String GetGroupPic(int groupID){
		
		if(groupID == GlbFinal.PUBLICID){
			
			return "images/fm.jpg";
		}
		GroupsForm theForm = new GroupsForm();
		theForm.setGroupID(groupID);

		GroupsServiceImpl service = new GroupsServiceImpl();
		ReturnMessage msg  = new ReturnMessage();		
		
		service.get(theForm, msg);
		
		if(theForm.getPic() > 0){
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			
			filesForm.setF_ID(theForm.getPic());
			
			fileservice.get(filesForm, msg);
			
			return filesForm.getFolder() + filesForm.getF_name();		
		}else{
			return "images/nopic.gif";
		}		
	}	
	
	/*根据群相册ID取群组名称
	 * 输入：GAlbumID
	 * 输出：GroupName
	 */
	public static String GetGroupNamebygAlbumID(int gAlbumID){
		
		GAlbumForm albumForm = new GAlbumForm();
		albumForm.setGalbumID(gAlbumID);
		ReturnMessage msg  = new ReturnMessage();		
		
		GAlbumServiceImpl  albumservice = new GAlbumServiceImpl();
		
		albumservice.get(albumForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			return "";
		}				
		GroupsForm theForm = new GroupsForm();
		theForm.setGroupID(albumForm.getGroupID());

		GroupsServiceImpl service = new GroupsServiceImpl();
		
		service.get(theForm, msg);
		
		return theForm.getName();
	}	
	/*根据群相册ID取封面照片
	 * 输入：GAlbumID
	 * 输出：GroupName
	 */
	public static String GetAlbumHeadPic(int gAlbumID){
		
		GPictureServiceImpl  picservice = new GPictureServiceImpl();
		GPictureForm picForm = new GPictureForm();
		ReturnMessage msg  = new ReturnMessage();		

		picForm.setQryGAlbumID(gAlbumID);
		picForm.setPage(new PageCtr(1, 1, "N", " 1=1 "));
		picForm.setQryOrderby(" IsHead, Mtime desc ");
		
		List list = picservice.adlist(picForm, msg);
		if(list == null || list.size() == 0){
			
			return "images/fm.jpg";
		}
		BaseGPicture base = (BaseGPicture)list.get(0);
		if(base.getPicName() > 0){
			FilesServiceImpl fileservice = new FilesServiceImpl();
			FilesForm filesForm = new FilesForm();
			
			filesForm.setF_ID(base.getPicName());
			
			fileservice.get(filesForm, msg);
			
			return filesForm.getFolder() + filesForm.getF_name();		
		}else{
			return "images/fm.jpg";
		}		
	}		
	
	/*根据群ID 用户ID取用户加入群标志
	 * 输入：groupID userID
	 * 输出：GroupName
	 */
	public static int GroupAddFlag(int groupID, int userID){
		
		int addFlag = 2;
		if(userID == 0){
		
			return addFlag;
		}
		GMemberServiceImpl memservice = new GMemberServiceImpl();
		GMemberForm memForm = new GMemberForm();
		ReturnMessage msg  = new ReturnMessage();		
		
		memForm.setUserID(userID);
		memForm.setGroupID(groupID);
		
		memservice.get(memForm, msg);
		if(msg.getErrCode() == 0 ){
			
			addFlag = 1;
		}			
		
		return addFlag;
	}	
	
	
	/*根据群ID 用户ID判断用户是否是创建者或管理员
	 * 输入：groupID userID
	 * 输出：GroupName
	 */
	public static boolean IsGroupManager(int groupID, int userID){
		
		boolean isman = false;
		if(userID == 0){
		
			return isman;
		}
		GMemberServiceImpl memservice = new GMemberServiceImpl();
		
		isman = memservice.IsManager(groupID, userID);

		return isman;
	}	
	
	/*
	 * 输入：VALUEMAPING 数组， GlbFinal的值
	 * 输出：message的值
	 */	
	public static String GetMsg(String arrayname, String strvalue){
	
		if(arrayname.equals("SEX_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.SEX_TYPE, strvalue);
		}else if(arrayname.equals("CERT_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.CERT_TYPE, strvalue);
		}else if(arrayname.equals("YESNO_FLAG")){

			return ValueMaping.GetMsg(ValueMaping.YESNO_FLAG, strvalue);
		}else if(arrayname.equals("STS_FLG")){

			return ValueMaping.GetMsg(ValueMaping.STS_FLG, strvalue);
		}else if(arrayname.equals("MSG_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.MSG_TYPE, strvalue);
		}else if(arrayname.equals("WEBMSG_FLAG")){

			return ValueMaping.GetMsg(ValueMaping.WEBMSG_FLAG, strvalue);
		}else if(arrayname.equals("STAR_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.STAR_TYPE, strvalue);
		}else if(arrayname.equals("SHXIAO_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.SHXIAO_TYPE, strvalue);
		}else if(arrayname.equals("USERSET_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.USERSET_TYPE, strvalue);
		}else if(arrayname.equals("FAMLIY_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.FAMLIY_TYPE, strvalue);
		}else if(arrayname.equals("LIVE_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.LIVE_TYPE, strvalue);
		}else if(arrayname.equals("JIGOU_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.JIGOU_TYPE, strvalue);
		}else if(arrayname.equals("NOTADV_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.NOTADV_TYPE, strvalue);
		}else if(arrayname.equals("NOTADV_SORT")){

			return ValueMaping.GetMsg(ValueMaping.NOTADV_SORT, strvalue);
		}else if(arrayname.equals("CHK_FLAG")){

			return ValueMaping.GetMsg(ValueMaping.CHK_FLAG, strvalue);
		}else if(arrayname.equals("FR_STS")){

			return ValueMaping.GetMsg(ValueMaping.FR_STS, strvalue);
		}else if(arrayname.equals("GMEMBER_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.GMEMBER_TYPE, strvalue);
		}else if(arrayname.equals("USER_STS")){

			return ValueMaping.GetMsg(ValueMaping.USER_STS, strvalue);
		}else if(arrayname.equals("USER_TYPE")){

			return ValueMaping.GetMsg(ValueMaping.USER_TYPE, strvalue);
		}
		
			
		return "";
	}

	/*
	 * 输入：VALUEMAPING 数组， GlbFinal的值
	 * 输出：message的值
	 */	
	public static String GetProperty(int assortID, int  propID){
	
		PropertyServiceImpl service = new PropertyServiceImpl();
			
		PropertyForm theForm = new PropertyForm();
		
		theForm.setAssortID(assortID);
		theForm.setPropID(propID);
		
		ReturnMessage msg = new ReturnMessage();
		
		service.get(theForm, msg);
		
		return theForm.getPropname();		
	}	
	
	/*
	 * 根据多个属性ID返回多个属性名称
	 * 输入：VALUEMAPING 数组， GlbFinal的值
	 * 输出：message的值
	 */	
	public static String GetPropertys(int assortID, String propIDs){
		
		PropertyServiceImpl service = new PropertyServiceImpl();
		
		return service.getPropNames(assortID, propIDs);
		
	}		
}

